package adapter;

import java.util.ArrayList;

import com.activity.MainAppActivity;
import com.activity.R;

import entities.Locate;
import android.content.Context;
import android.graphics.Color;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdaperBookmarkRecent extends ArrayAdapter<Locate>{
	private Context context;
	private int resId;
	private ArrayList<Locate> listLocate;
	private ActionMode actionMode;
	private MainAppActivity mainAppActivity;
	public AdaperBookmarkRecent(Context context, int resId,
			ArrayList<Locate> objects) {
		super(context, resId, objects);
		// TODO Auto-generated constructor stub
		this.context=context;
		this.resId=resId;
		this.listLocate=objects;
		mainAppActivity=(MainAppActivity)context;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView==null){
			LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(resId, null);
		}
		TextView txtName=(TextView) convertView.findViewById(R.id.txt_bm_name);
		txtName.setTypeface(MainAppActivity.typeFaceFont);
		
		TextView txtAddress=(TextView) convertView.findViewById(R.id.txt_bm_address);
		txtAddress.setTypeface(MainAppActivity.typeFaceFont);
		
		TextView txtDateAdded=(TextView) convertView.findViewById(R.id.txt_bm_date_added);
		txtDateAdded.setTypeface(MainAppActivity.typeFaceFont);
		CheckBox checkBox=(CheckBox) convertView.findViewById(R.id.checkbox_checked);
		
		Locate locate=listLocate.get(position);
		txtName.setText(locate.getName());
		if(locate.getDateAdded()!=null)
			txtDateAdded.setText(locate.getDateAdded());
		txtAddress.setText(locate.getAdress());
		checkBox.setChecked(locate.getIsChecked());
		final LinearLayout layout=(LinearLayout) convertView.findViewById(R.id.layout_bookmark);
		//cho phep goi onitemclick
		checkBox.setFocusable(false);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			private int count=0;
			private Locate locate=listLocate.get(position);
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					layout.setBackgroundColor(Color.BLACK);
					if(actionMode==null){
						actionMode=mainAppActivity.startActionMode(mainAppActivity.callback);
					}
					locate.setIsChecked(true);
				}
				else
				{
					layout.setBackgroundColor(context.getResources().getColor(R.color.background));
					locate.setIsChecked(false);
					for(Locate locate:listLocate){
						if(locate.getIsChecked())
							count++;
					}
					if(count==0&&actionMode!=null){
						actionMode.finish();
						actionMode=null;
					}
						
				}	
				
			}
		});
		return convertView;
	}
	
}
